package com.scs.application.modules.fsd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.fsd.dto.SaveStockDTO;
import com.scs.application.modules.fsd.entity.FsdStock;
import com.scs.application.modules.fsd.mapper.FsdStockMapper;
import com.scs.application.modules.fsd.service.FsdStockService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 智能柜库存
 */
@Service
public class FsdStockServiceImpl extends BaseServiceImpl<FsdStockMapper, FsdStock> implements FsdStockService {
    @Override
    public List<FsdStock> getStockByStationCode(String stationCode) {
        return this.list(Wrappers.<FsdStock>query().eq("station_code",stationCode));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean saveStock(String stationCode, List<SaveStockDTO> stockList) {
        remove(Wrappers.<FsdStock>query().eq("station_code",stationCode));
        Collection<FsdStock> fsdStocks = new ArrayList<>();
        for (SaveStockDTO stockDTO : stockList){
            FsdStock fsdStock = new FsdStock();
            fsdStock.setStationCode(stationCode);
            fsdStock.setRfid(stockDTO.getRfid());
            fsdStock.setChestNo(stockDTO.getChestNo());
            fsdStocks.add(fsdStock);
        }
        saveBatch(fsdStocks);
        return true;
    }
}
